//
//  ZCMeVC.m
//  zctx
//
//  Created by ZhengDarren on 15/9/28.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeVC.h"
#import "ZCLoginVC.h"
#import "ZCMeRaiseVC.h"
#import "ZCMeNotificationCenterVC.h"
#import "JZMTBtnView.h"

#import "ZCMeVCOneCell.h"
#import "TableViewTwoCell.h"

#import "ZCstrategyVC.h"  //攻略
#import "ZCredpacketsVC.h" //我的红包
#import "ZCMeDataVC.h"      //我的资料
#import "ZCMeFeedbackVC.h"  //反馈意见
#import "ZCAboutVC.h"  //关于我们
#import "ZCSetupVC.h"   //设置

#import "ZCfollowVC.h"
#import "ZCHealthyVC.h"
#import "ZCCollectionVC.h"

#import "MeSecondCell.h"
#import "MezeroCell.h"

#import "ActionView.h"
@interface ZCMeVC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,MeSecondCellDelegate>{
    JZMTBtnView *btnView;
    UILabel *namelable;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIButton * rightButton;

@property (nonatomic,strong)NSDictionary *dataDic;
@property (nonatomic,strong)NSArray *notifictionArray;
@property (nonatomic,strong)NSString *isread;//是否有通知

@end

@implementation ZCMeVC

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"userheader" object:nil];
     [[NSNotificationCenter defaultCenter]removeObserver:self name:@"login" object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMeVC"];
    [MTA trackPageViewBegin:@"ZCMeVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self requestData];
        [self requestNotifictiondata];//请求通知数据
    });
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMeVC"];
    [MTA trackPageViewEnd:@"ZCMeVC"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的";
    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];
    self.dataDic = [[NSDictionary alloc]init];
    [self initnavegation];
    [self initTableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTableView) name:@"login" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadHeaderTableView) name:@"userheader" object:nil];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewTwoCell" bundle:nil] forCellReuseIdentifier:@"metwo"];
    
    self.tableView.mj_header = [MJDIYHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self requestData];
    }];
}

#pragma mark 导航栏
-(void)initnavegation{
   self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 32, 40)];
    [self.rightButton setImage:[UIImage imageNamed:@"me_notification_is_no"] forState:UIControlStateNormal];
    
    [self.rightButton addTarget:self action:@selector(notificationClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
}
-(void)notificationClick{
    
    if (UserModel.password.length >1) {
        ZCMeNotificationCenterVC *centerVC = [[ZCMeNotificationCenterVC alloc]init];
        centerVC.hidesBottomBarWhenPushed = YES;
        centerVC.dataArray = self.notifictionArray;
        [self.navigationController pushViewController:centerVC animated:YES];
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];

    }
}
-(void)buttonClick{
    NSLog(@"sdf");
}

#pragma mark 我的数据
-(void)requestData{
    [NetworkManager requestGetMemberInfoWithMid:UserModel.mid password:UserModel.password device_token:theAppdelegate.deviceToken success:^(NSDictionary *dictResponse) {
        [self.tableView.mj_header endRefreshing];
        if ([dictResponse[@"content"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict =(NSDictionary *) dictResponse[@"content"];
            self.dataDic = [dict copy];
            if ([[dict objectForKey:@"avatar"] isKindOfClass:[NSString class]]) {
                NSString *stravatar = (NSString *)[dict objectForKey:@"avatar"];
                [UserModel setAvatar:stravatar];
            }
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

-(void)initTableViewHeader{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,130)];
    btnView = [[JZMTBtnView alloc]initWithFrame:CGRectMake(0, 0,100, 100)];
    [headView addSubview:btnView];
    headView.backgroundColor = [UIColor whiteColor];
    btnView.font = [UIFont systemFontOfSize:14];
    btnView.imageStr =@"projectdetail_icon";
    if (UserModel.password.length>1) {
        btnView.titleStr = UserModel.username;
    }else{
        btnView.titleStr = @"请登录";
    }
    btnView.center = headView.center;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickLoginButton)];
    [btnView addGestureRecognizer:tap];
    self.tableView.tableHeaderView = headView;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self tableViewFooterView];
}

//帮助我们进步
-(void)tableViewFooterView{
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,80)];
    UIButton *helpButton = [[UIButton alloc]init];
    [footerView addSubview:helpButton];
    [helpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footerView.centerX).with.offset(0);
        make.centerY.equalTo(footerView.centerY).with.offset(0);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(50);
    }];
    helpButton.titleLabel.font = [UIFont systemFontOfSize:16];
    helpButton.backgroundColor = [UIColor clearColor];
    [helpButton setTitle:@"帮助我们进步" forState:UIControlStateNormal];
    [helpButton setTitleColor:MainColor forState:UIControlStateNormal];
    [helpButton addTarget:self action:@selector(Clickhelp) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView = footerView;
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0||section==1) {
        return 1;
    }else{
        return 6;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        MezeroCell *cell = [MezeroCell MezeroCellWithTableView:tableView];
        return cell;
    }else if(indexPath.section==1){
        static NSString *cellIndentifier = @"mesecond";
        MeSecondCell *cell = [[MeSecondCell alloc]initWithStyle:
                    UITableViewCellStyleDefault reuseIdentifier:cellIndentifier andDict:self.dataDic];
        cell.delegate = self;
        return cell;
    }
    else{
        TableViewTwoCell *cell = [TableViewTwoCell tableViewTwoCellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 145;
    }else if(indexPath.section==1){
        return 80;
    }else{
        return 44;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.000001;
    }else{
        return 5;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        [self ClickLoginButton];
    }else if (indexPath.section==1){
        if (UserModel.password.length>1) {
            if (indexPath.row==0) {
                ZCMeRaiseVC *raiseVC = [[ZCMeRaiseVC alloc]init];
                raiseVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:raiseVC animated:YES];
            }else{
                ZCMeNotificationCenterVC *centerVC = [[ZCMeNotificationCenterVC alloc]init];
                centerVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:centerVC animated:YES];
            }
        }else{
            ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
            loginVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:loginVC animated:YES];
        }
    }else if (indexPath.section==2){
        NSLog(@"index = %ld ",indexPath.row);
        
        if (indexPath.row==4) {
            ZCMeFeedbackVC *zcvc = [[ZCMeFeedbackVC alloc]init];
            zcvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:zcvc animated:YES];
        }else{
            if (UserModel.password.length>1) {
                if (indexPath.row==0) {
                    ZCfollowVC *follow = [[ZCfollowVC alloc]init];
                    follow.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:follow animated:YES];
                }
                else if (indexPath.row==1) {
                    ZCCollectionVC * collect = [[ZCCollectionVC alloc]init];
                    collect.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:collect animated:YES];
                }else if (indexPath.row==2){
                    ZCstrategyVC * collect = [[ZCstrategyVC alloc]init];
                    collect.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:collect animated:YES];
                }else if (indexPath.row ==3){
                    ActionView *action = [[ActionView alloc]initWithFrame:CGRectZero];
                    action.isweb =6;
                    [action showInView:self.view];
                }
                else{
                    ZCSetupVC *zcmevc = [[ZCSetupVC alloc]init];
                    zcmevc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController: zcmevc animated:YES];
                }
            }else{
                ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
                loginVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:loginVC animated:YES];
            }
        }
        }else{
        if (indexPath.row==1) {

            
        }else{
            ZCAboutVC *zcabout = [[ZCAboutVC alloc]init];
            zcabout.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:zcabout animated:YES];
        }
    }
    [self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.3];
}

-(void)unselectCell:(id)sender{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

-(void)reloadTableView{
    [self.tableView reloadData];
}

-(void)reloadHeaderTableView{
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark 点击了3个按钮
-(void)didSelectMeSecondCellIndex:(NSInteger)index{
    NSLog(@"index = %ld",index);
    if (UserModel.password.length>1) {
        if (index==0) {
            ZCredpacketsVC *redvc = [[ZCredpacketsVC alloc]init];
            redvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:redvc animated:YES];
        }else if (index==1){
            ZCMeRaiseVC *raiseVC = [[ZCMeRaiseVC alloc]init];
            raiseVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:raiseVC animated:YES];
        }else{
            ZCHealthyVC *healthy = [[ZCHealthyVC  alloc]init];
            healthy.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:healthy animated:YES];
        }
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

#pragma mark 登入按钮
-(void)ClickLoginButton{
    
    if (UserModel.password.length>1) {
        
        ZCMeDataVC *zcvc = [[ZCMeDataVC alloc]init];
        zcvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:zcvc animated:YES];
        
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

-(void)Clickhelp{
    if (UserModel.password.length>1) {
        ZCMeFeedbackVC *zcvc = [[ZCMeFeedbackVC alloc]init];
        zcvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:zcvc animated:YES];
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }

}

#pragma mark 请求通知数据
-(void)requestNotifictiondata{
    
    [NetworkManager requestMemberMessageWithMid:UserModel.mid password:UserModel.password success:^(NSDictionary *ArrayResponse) {
        if ([[ArrayResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)[ArrayResponse objectForKey:@"content"];
            self.notifictionArray = [array copy];
            
            for (NSDictionary *dict in array) {
                NSString *isread = [NSString stringWithFormat:@"%@",[dict objectForKey:@"isread"]];
                if ([isread isEqualToString:@"0"]) {
                    self.isread = @"1";
                    [self isreadNotifiction];
                    return;
                }else{
                    self.isread = @"0";
                    [self isreadNotifiction];
                }
            }
        }
    } failure:^(NSError *error) {
    }];
}

-(void)isreadNotifiction{
    if ([self.isread isEqualToString:@"1"]) {
        [self.rightButton setImage:[UIImage imageNamed:@"me_nofication"] forState:UIControlStateNormal];
    }else{
        [self.rightButton setImage:[UIImage imageNamed:@"me_notification_is_no"] forState:UIControlStateNormal];
    }
}

-(NSArray*)notifictionArray{
    if (!_notifictionArray) {
        _notifictionArray = [[NSArray alloc]init];
    }
    return _notifictionArray;
}

@end
